package com.control;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import com.mode.Shape;
import com.mode.ShapeFactory;
import com.mode.Wall;
import com.view.GamePn;

public class MainFrame extends JFrame implements ActionListener{

	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Shape shape;
	private Wall wall;

	public MainFrame() {
		setTitle("俄罗斯方块");
		setSize(400, 600);
		setResizable(false);
		setLayout(null);
		Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
		int screenX=(int) screenSize.getWidth();
		int screenY=(int)screenSize.getHeight();
		int x=screenX/2-this.getWidth()/2;
		int y=screenY/2-this.getHeight()/2;
		setLocation(x, y);
		
		shape=ShapeFactory.createShape();
		wall=new Wall();
		GamePn gp=new GamePn(wall);
		gp.setBounds(5, 5, this.getWidth()-105, this.getHeight()-40);
		gp.setBackground(new Color(0xF0F8FF));
		add(gp);
		shape.addObserver(gp);
		
		this.addKeyListener(new KeyAdapter() {
			
			@Override
			public void keyPressed(KeyEvent e) {
				int code=e.getKeyCode();
				
				switch (code) {
				case KeyEvent.VK_LEFT:
					shape.moveLeft();
					break;
				case KeyEvent.VK_RIGHT:
					shape.moveRight();
					break;
				case KeyEvent.VK_UP:
					shape.rotate();
					break;
				default:
					break;
				}
				
			}
			
			
			
		});
		
		shape.startMove();
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public void createNode(){
		
	}
	
	public static void main(String[] args) throws InvocationTargetException, InterruptedException {
		SwingUtilities.invokeAndWait(new Runnable() {
			
			@Override
			public void run() {
				new MainFrame();
			}
		});
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
	}

}
